﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UM_SERVER.Net;
using UM_SERVER.Packet.InPacket;
using UM_SERVER.Session;
using UM_SERVER.User;
using UM_SERVER.Triggers;
using UM_SERVER.Triggers.TriggerObject;

namespace UM_SERVER.ProcessModel
{
    class ProcessUserModel
    {
        private static String PackageBody;
        private static String Protocol;

        /// <summary>
        ///     将指定用户添加到上线集合中
        /// </summary>
        /// <param name="username" type="string">
        ///     <para>
        ///         用户名
        ///     </para>
        /// </param>
        /// <param name="Mession" type="int">
        ///     <para>
        ///         用户权限
        ///     </para>
        /// </param>
        /// <param name="inpackage" type="UM_SERVER.Packet.InPacket.LoginPacket">
        ///     <para>
        ///         登陆包
        ///     </para>
        /// </param>
        /// <returns>返回null , 则表示添加失败</returns>
        public static String AddUserToOnlineCollection(String username, int Mession, LoginPacket inpackage, UM_SERVER.Packet.InPacket.LoginPacket.LoginState LoginLive)
        {
            User.User user = null;
            switch (Mession)
            {
                //普通用户
                case 0:
                    //封装用户
                    user = new Normal_User(username);
                    user.Mession = (Level.MessionLevel)Mession;
                    user.IpAddress = inpackage.GetUserIpAddress();
                    user.OnlineTime = DateTime.Now;
                    user.TalkPort = inpackage.GetTalkPort();
                    user.Username = username;
                    user.VideoPort = inpackage.GetVideoPort();
                    user.VoicePort = inpackage.GetVoicePort();
                    user.TransferFilePort = inpackage.GetTransferFilePort();
                    user.LoginLive = LoginLive;
                    //当前用户存在了
                    if (Globle.userManager.Equals(user))
                    {
                        Globle.userManager.CoverLoginUser(user);
                        TriggerUserOnline(user);
                        return SessionProcessModel.AddNewUserBaseSession(user);
                    }
                    //不存在
                    else
                    {
                        //打入用户集合
                        Globle.userManager.Add(user);
                        TriggerUserOnline(user);
                        return SessionProcessModel.AddNewUserBaseSession(user);
                    }
                    break;
            }
            return null;
        }

        private static void TriggerUserOnline(User.User user)
        {
            if (user != null)
            {
                //**************************************************************************************
                //                              开始触发用户上线触发器
                //**************************************************************************************
                UserOnlineTrigger trigger = Globle.triggerManager.GetTriggerByKey("UserOnlineTrigger") as UserOnlineTrigger;
                //开始触发
                trigger.Trigger(user.Username);
            }
        }
        /// <summary>
        ///    强制指定用户下线
        /// </summary>
        /// <param name="user">用户</param>
        /// <param name="UserSession">用户原有会话</param>
        public static void SetUserOffline(User.User user, Session.Session UserSession)
        {
            byte[] Username = Encoding.Default.GetBytes(user.Username);
            byte[] UsernameLength = BitConverter.GetBytes(Username.Length);
            byte[] Session = Encoding.Default.GetBytes(UserSession.GetSessioCode());
            byte[] SessionLength = BitConverter.GetBytes(Session.Length);
            byte[] packagebody = new byte[Username.Length + Session.Length + 8];
            int Offset = 0;
            //复制用户名长度
            Array.ConstrainedCopy(UsernameLength, 0, packagebody, Offset, 4);
            Offset += 4;
            //复制用户名
            Array.ConstrainedCopy(Username, 0, packagebody, Offset, Username.Length);
            Offset += Username.Length;
            //会话ID长度
            Array.ConstrainedCopy(SessionLength, 0, packagebody, Offset, 4);
            Offset += 4;
            //会话ID
            Array.ConstrainedCopy(Session, 0, packagebody, Offset, Session.Length);
            Offset += Session.Length;
            //得到协议
            byte[] protocol1 = ProtocolFactory.ProtocolFactory.GetProtocol("SetOffline",
                                                                          (int)Packet.Protocol.OUT_NetworkCommand.SET_USER_OFFLINE,
                                                                          (int)Packet.Protocol.NetworkProtocol.Base,
                                                                          packagebody,
                                                                          ProtocolFactory.ProtocolFactory.ACK.No,
                                                                          ProtocolFactory.ProtocolFactory.ENCRYPT.No, "");
            NetControl.Send(protocol1, user.TalkPort, user.IpAddress);
        }

        /// <summary>
        ///      修改用户在线状态成功回复
        /// </summary>
        /// <param name="inpackage">更改用户在线状态请求包</param>
        public static void ACK_SucessfulChanageUserOnlineState(ChanageUserOnlineStatePacket inpackage)
        {
            byte[] Username = Encoding.Default.GetBytes(inpackage.GetUsername());
            byte[] UsernameLength = BitConverter.GetBytes(Username.Length);
            byte[] Session = Encoding.Default.GetBytes(inpackage.GetSessionCode());
            byte[] SessionLength = BitConverter.GetBytes(Session.Length);
            byte[] UpdateState = BitConverter.GetBytes(true);
            byte[] packagebody = new byte[Username.Length + Session.Length + 9];
            int Offset = 0;
            //复制用户名长度
            Array.ConstrainedCopy(UsernameLength, 0, packagebody, Offset, 4);
            Offset += 4;
            //复制用户名
            Array.ConstrainedCopy(Username, 0, packagebody, Offset, Username.Length);
            Offset += Username.Length;
            //会话ID长度
            Array.ConstrainedCopy(SessionLength, 0, packagebody, Offset, 4);
            Offset += 4;
            //会话ID
            Array.ConstrainedCopy(Session, 0, packagebody, Offset, Session.Length);
            Offset += Session.Length;
            //更新状态
            Array.ConstrainedCopy(UpdateState, 0, packagebody, Offset, 1);
            Offset += 1;
            //得到协议
            byte[] protocol1 = ProtocolFactory.ProtocolFactory.GetProtocol("ReplyChanageUserOnlineState",
                                                                          (int)Packet.Protocol.OUT_NetworkCommand.REPLY_CHANAGE_USER_ONLINE_STATE_PACKAGE,
                                                                          (int)Packet.Protocol.NetworkProtocol.Base,
                                                                          packagebody,
                                                                          ProtocolFactory.ProtocolFactory.ACK.No,
                                                                          ProtocolFactory.ProtocolFactory.ENCRYPT.No, "");
            NetControl.Send(protocol1, inpackage.GetTalkPort(), inpackage.GetUserIpAddress());
        }

        /// <summary>
        ///      修改用户在线状态失败回复
        /// </summary>
        /// <param name="inpackage">更改用户在线状态请求包</param>
        public static void ACK_FailChanageUserOnlineState(ChanageUserOnlineStatePacket inpackage)
        {
            byte[] Username = Encoding.Default.GetBytes(inpackage.GetUsername());
            byte[] UsernameLength = BitConverter.GetBytes(Username.Length);
            byte[] Session = Encoding.Default.GetBytes(inpackage.GetSessionCode());
            byte[] SessionLength = BitConverter.GetBytes(Session.Length);
            byte[] UpdateState = BitConverter.GetBytes(false);
            byte[] packagebody = new byte[Username.Length + Session.Length + 9];
            int Offset = 0;
            //复制用户名长度
            Array.ConstrainedCopy(UsernameLength, 0, packagebody, Offset, 4);
            Offset += 4;
            //复制用户名
            Array.ConstrainedCopy(Username, 0, packagebody, Offset, Username.Length);
            Offset += Username.Length;
            //会话ID长度
            Array.ConstrainedCopy(SessionLength, 0, packagebody, Offset, 4);
            Offset += 4;
            //会话ID
            Array.ConstrainedCopy(Session, 0, packagebody, Offset, Session.Length);
            Offset += Session.Length;
            //更新状态
            Array.ConstrainedCopy(UpdateState, 0, packagebody, Offset, 1);
            Offset += 1;
            //得到协议
            byte[] protocol1 = ProtocolFactory.ProtocolFactory.GetProtocol("ReplyChanageUserOnlineState",
                                                                          (int)Packet.Protocol.OUT_NetworkCommand.REPLY_CHANAGE_USER_ONLINE_STATE_PACKAGE,
                                                                          (int)Packet.Protocol.NetworkProtocol.Base,
                                                                          packagebody,
                                                                          ProtocolFactory.ProtocolFactory.ACK.No,
                                                                          ProtocolFactory.ProtocolFactory.ENCRYPT.No, "");
            NetControl.Send(protocol1, inpackage.GetTalkPort(), inpackage.GetUserIpAddress());
        }

        /// <summary>
        ///     回馈用户心跳信息包
        /// </summary>
        /// <param name="inpackage">用户心跳请求包</param>
        public static void ACK_UserHeartBreath(UserHeartBreathPacket inpackage)
        {
            byte[] Username = Encoding.Default.GetBytes(inpackage.GetUsername());
            byte[] UsernameLength = BitConverter.GetBytes(Username.Length);
            byte[] Session = Encoding.Default.GetBytes(inpackage.GetSessionCode());
            byte[] SessionLength = BitConverter.GetBytes(Session.Length);
            byte[] Result = BitConverter.GetBytes(true);
            byte[] packagebody = new byte[Username.Length + Session.Length + 9];
            int Offset = 0;
            //复制用户名长度
            Array.ConstrainedCopy(UsernameLength, 0, packagebody, Offset, 4);
            Offset += 4;
            //复制用户名
            Array.ConstrainedCopy(Username, 0, packagebody, Offset, Username.Length);
            Offset += Username.Length;
            //复制会话ID长度
            Array.ConstrainedCopy(SessionLength, 0, packagebody, Offset, 4);
            Offset += 4;
            //复制会话ID
            Array.ConstrainedCopy(Session, 0, packagebody, Offset, Session.Length);
            Offset += Session.Length;
            //复制心跳成功标示
            Array.ConstrainedCopy(Result, 0, packagebody, Offset, 1);
            Offset += 1;
            //得到协议
            byte[] protocol1 = ProtocolFactory.ProtocolFactory.GetProtocol("ReplyUserHeartBreath",
                                                                          (int)Packet.Protocol.OUT_NetworkCommand.REPLY_USER_HEART_BREATH_PACKAGE,
                                                                          (int)Packet.Protocol.NetworkProtocol.Base,
                                                                          packagebody,
                                                                          ProtocolFactory.ProtocolFactory.ACK.No,
                                                                          ProtocolFactory.ProtocolFactory.ENCRYPT.No, "");
            NetControl.Send(protocol1, inpackage.GetTalkPort(), inpackage.GetUserIpAddress());
        }

    }
}
